package org.springside.examples.miniweb.web.cm;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.examples.miniweb.entity.cm.Company;
import org.springside.examples.miniweb.service.ServiceException;
import org.springside.examples.miniweb.service.cm.CompanyManager;
import org.springside.examples.miniweb.service.dictionary.DictionaryClassManager;
import org.springside.examples.miniweb.service.dictionary.ProvinceManager;
import org.springside.examples.miniweb.web.CrudActionSupport;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.hibernate.HibernateUtils;
import org.springside.modules.web.struts2.Struts2Utils;

@Namespace("/cm")
//定义名为reload的result重定向到company.action, 其他result则按照convention默认.
@Results( { 
	@Result(name = CrudActionSupport.RELOAD, location = "listCustom.action", type = "redirect")
})
public class CustomAction  extends CrudActionSupport<Company> {
private static final long serialVersionUID = 1L;
	
	@Autowired
	private CompanyManager companyManager;
	@Autowired
	private DictionaryClassManager dictionaryClassManager;
	@Autowired
	private ProvinceManager provinceManager;
	
	//-- 页面元素--//
	private Long id;
	private Company entity;
	private Page<Company> page = new Page<Company>(5);//每页5条数�?	
	//-- ModelDriven与Preparable函数--//
	public void setId(Long id) {
		this.id = id;
	}

	public Company getModel() {
		return entity;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = companyManager.getCompany(id);
		} else {
			entity = new Company();
		}
	}

	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = HibernateUtils.buildPropertyFilters(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = companyManager.searchCompany(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		ServletActionContext.getRequest().setAttribute("list", dictionaryClassManager.getAllDictionaryClass());
		ServletActionContext.getRequest().setAttribute("provinceList", provinceManager.getAllProvince());
		return INPUT;
	}
	
	@Override
	public String save() throws Exception {
		companyManager.saveCompany(entity);
		addActionMessage("保存成功");//TODO
		return RELOAD;
	}
	
	@Override
	public String delete() throws Exception {
		try {
			companyManager.deleteCompany(id);
			addActionMessage("删除成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除失败");
		}
		return RELOAD;
	}
	
	//-- 页面元素访问函数 --//
	/**
	 * list页面显示用户分页列表.
	 */
	public Page<Company> getPage() {
		return page;
	}
		
}
